<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=445004
-->
<head>
  <title>Test for Bug 445004</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=445004">Mozilla Bug 445004</a>
<div id="content" style="display: none">
  
</div>
<pre id="test">
<script type="application/javascript">

/** Test for Bug 445004 **/
is(window.location.hostname, "mochi.test", "Unexpected hostname");
is(window.location.port, "8888", "Unexpected port; fix testcase");

SimpleTest.waitForExplicitFinish();

var loads = 1;

function loadStarted() {
  ++loads;
}
function loadEnded() {
  --loads;
  if (loads == 0) {
    doTest();
  }
}

window.onload = loadEnded;

function getMessage(evt) {
  ok(evt.data == "start" || evt.data == "end", "Must have start or end");
  if (evt.data == "start")
    loadStarted();
  else
    loadEnded();
}

window.addEventListener("message", getMessage, false);

function checkURI(uri, name, type) {
  var host = uri.match(/^http:\/\/([a-z.0-9]*)/)[1];
  var file = uri.match(/([^\/]*).png$/)[1];
  is(host, file, "Unexpected base URI for test " + name +
     " when testing " + type);
}

function checkFrame(num) {
  // Just snarf our data
  var outer = SpecialPowers.wrap(window.frames[num]);
  name = outer.name;

  is(outer.document.baseURI,
     "http://example.org/tests/dom/html/test/bug445004-outer.html",
     "Unexpected base URI for " + name);

  var iswrite = name.match(/write/);

  var inner = outer.frames[0];
  if (iswrite) {
    is(inner.document.baseURI,
       "http://example.org/tests/dom/html/test/bug445004-outer.html",
       "Unexpected inner base URI for " + name);
  } else {
    is(inner.document.baseURI,
       "http://test1.example.org/tests/dom/html/test/bug445004-inner.html",
       "Unexpected inner base URI for " + name);
  }

  var isrel = name.match(/rel/);
  var offsite = name.match(/offsite/);

  if (!iswrite) {
    if ((isrel && !offsite) || (!isrel && offsite)) {
      is(inner.location.hostname, outer.location.hostname,
         "Unexpected hostnames for " + name);
    } else {
      isnot(inner.location.hostname, outer.location.hostname,
            "Unexpected hostnames for " + name);
    }
  }

  checkURI(inner.frames[0].location.href, name, "direct location");
  checkURI(inner.frames[1].document.getElementsByTagName("img")[0].src,
           name, "direct write");
  if (!iswrite) {
    is(inner.frames[1].location.hostname, inner.location.hostname,
       "Incorrect hostname for " + name + " direct write")
  }
  checkURI(inner.frames[2].location.href, name, "indirect location");
  checkURI(inner.frames[3].document.getElementsByTagName("img")[0].src,
           name, "indirect write");
  if (!iswrite) {
    is(inner.frames[3].location.hostname, outer.location.hostname,
       "Incorrect hostname for " + name + " indirect write")
  }
  checkURI(inner.document.getElementsByTagName("img")[0].src,
           name, "direct image load");
}


function doTest() {
  for (var num = 0; num < 5; ++num) {
    checkFrame(num);
  }

  SimpleTest.finish();
}

</script>
</pre>
<p id="display">
  <iframe
     src="http://example.org/tests/dom/html/test/bug445004-outer-rel.html"
     name="bug445004-outer-rel.html"></iframe>
  <iframe
     src="http://test1.example.org/tests/dom/html/test/bug445004-outer-rel.html"
     name="bug445004-outer-rel.html offsite"></iframe>
  <iframe
     src="http://example.org/tests/dom/html/test/bug445004-outer-abs.html"
     name="bug445004-outer-abs.html"></iframe>
  <iframe
     src="http://test1.example.org/tests/dom/html/test/bug445004-outer-abs.html"
     name="bug445004-outer-abs.html offsite"></iframe>
  <iframe
     src="http://example.org/tests/dom/html/test/bug445004-outer-write.html"
     name="bug445004-outer-write.html"></iframe>
</p>
</body>
</html>
